/**
 * ...
 * @author Axel Huizinga - axel@cunity.me
 */

package;

import flash.Boot;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.geom.Rectangle;
import flash.Lib;
import flash.system.ApplicationDomain;
import flash.xml.XML;
import haxe.Log;
//import me.cunity.animation.ScreenPlay;
import me.cunity.animation.TimeLine;
import me.cunity.debug.Out;
import me.cunity.layout.Alignment;
import me.cunity.tools.sandy.BitmapResource;
import me.cunity.ui.mask.Stripes;
import sandy.materials.Appearance;
import sandy.materials.BitmapMaterial;
import sandy.core.Scene3D;
import sandy.core.scenegraph.Camera3D;
import sandy.core.scenegraph.Group;

class Globe  extends MovieClip
//class Main extends Sprite, implements ScreenPlay
{

	
	public var globe:Planet;
	public var nextCall:Dynamic;
	public var storyBoard:Hash<Dynamic>;
	public var timeLine(default, null):TimeLine;
	
	public static function main() 
	{
		Out.traceTarget = DebugOutput.CONSOLE;
		Out.traceTarget = DebugOutput.NATIVE;
		Log.trace = Out._trace;		
		var m = new Globe();

	}
	
	public function new()
	{
		//Out.traceTarget = DebugOutput.CONSOLE;
		//Log.trace = Out._trace;
		super();
		//screenPlay = new ScreenPlay();
		storyBoard  = new Hash();
		if (Lib.current.stage != null)
		{
			Lib.current.addChild(this);
			init(400, 320);
		}
	}
	
	public function init(w:Float = 400, h:Float = 320, location:Dynamic = null)
	{
		var bBox: Rectangle = new Rectangle(0, 0, w, h);
		globe = new Planet(this);
		//addChild(globe);
		trace(Std.string(location));
		globe.init(createWorld(this, bBox), location);
		//space.setBackground(new Appearance(new  BitmapMaterial(new BitmapResource('Sky').drawScaledData(bBox, Alignment.TOP))), bBox);
		var bData:BitmapData = new BitmapResource('Sky').bmp.bitmapData;
		trace(bData.rect.toString());
		
		globe.setBackground(w, h, new Appearance(new  BitmapMaterial(bData)));
		globe.draw();
		
		//tL.add(globe.setGlow);
		//tL.add(globe.planetAction);
		//globe.planetAction(tL);
		trace(Lib.current.name + ' == ' + Lib.current.parent + ':'  + Std.string(Lib.current.parent == Lib.current.stage));
		if (Lib.current.stage != null)
		{
			trace('adding to stage');
			//Lib.current.addChild(this);
			timeLine = new TimeLine(null, new XML('<TimeLine  storyBoard="go,rainbow" />'));
			timeLine.addTween(null, new XML('<STween start="500"  id="go" />'), globe.action);
			timeLine.addTween(null, new XML('<STween start="0"  id="rainbow" />'), globe.rainbow);
			//globe.action();
			timeLine.next();
			nextCall = timeLine.next();
		}
		
		else
		{
			storyBoard.set('go', globe.action);
			storyBoard.set('rainbow', globe.rainbow);
		}
	}
	
	static function createWorld(clip:Sprite = null, box:Rectangle = null):Scene3D
	{
		if (box == null)
		{
			//box = new Rectangle(stage.getBounds(this));
			box = Lib.current.stage.getBounds(Lib.current);
		}
		
		if (clip == null)
			clip = Lib.current;
		trace(box.toString());
		var world:Scene3D = new Scene3D ("scene", clip, new Camera3D(Std.int(box.width), Std.int(box.height)), new Group("root"));
		world.camera.x = world.camera.y = 0;
		world.camera.z = -1000;
		world.root.addChild(world.camera);
		world.rectClipping = true;
		return world;
	}
	
}